package com.example.vv.controller;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

/**
 * @author w.dehai.2024/7/15.09:26
 */
@RestController
@RequestMapping("/dc")
public class DeferredController {
    @GetMapping("/get_config")
    public DeferredResult<String> getConfig(Long sleep) {
        DeferredResult<String> resp = new DeferredResult<>(sleep, "time out.");
        CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            resp.setResult(String.valueOf(new Random().nextInt(1000)));
            return null;
        });
        return resp;
    }
}
